<?php


namespace App\Customer\Controller;

use App\Service\Dict;
use App\Service\DictConstant;
use App\Customer\Model\CaseModel;
use App\Customer\Model\ResumeRecommend as RecommendModel;
use App\Customer\Model\ResumeRecommendDetail as RRDetailModel;
use EchoPHP\Http\Request;

class CaseInfo extends Controller {

    /*
     * 获取推荐批次对应的case列表数据
     */
    public function getRecommendCaseList(Request $request) {
        //2020-7-6 去除b端职位列表页废弃
        return $this->json(
            ['status' => false,
                'message' => '无此数据',
                'caseinfo' => [],
            ]
        );

        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);

        $id = $request->getParam('recommendid');
        $duetime = $request->getParam('duetime');
        $isTest = $request->getParam('istest', '');

        //获取推荐的caseid
        $field = "caseid,IF(firstopentime='0000-00-00 00:00:00','',firstopentime) as firstopentime";
        $recommendModel = new RecommendModel();
        $caseIds = $recommendModel->getCaseIdFromRecommend($id, $field);

        $caseResumeNum = 0;
        if (!isset($caseIds["caseid"]) || $caseIds["caseid"] == '') {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'caselist' => [],
                ]
            );
        }

        if (empty($isTest)) {
            $recommendModel->setRecommendReadTime($id);
        }

        $caseModel = new CaseModel();
        //获取推荐的case列表数量
        $count = $caseModel->getCaseListCount($caseIds["caseid"]);
        $result = [];
        if ($count < 1) {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'caselist' => [],
                ]
            );
        }

        list($pageNum, $rowFrom, $rowTo) = $caseModel->paginationParameters($pageNum, $pageLimit);
        $field = '';
        $result = $caseModel->getCaseList($field, $caseIds["caseid"], $rowFrom, $rowTo);
        if (!$result) {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'caseinfo' => [],
                ]
            );
        }
        $attributes = [
            'area' => [DictConstant::DD_CITY_COUNTY, '']
        ];

        //翻译城市
        $dict = new Dict();
        $dict->rowTrans($result, $attributes, true, ',', ' ');

        $resultCaseInfo = [];
        foreach ($result as $key => $item) {
            $item['num'] = '0人';
            $item['status01'] = $item['status02'] = $item['status04'] = $item['status05'] = $item['status06'] = '0人';
            $resultCaseInfo[$item['caseid']] = $item;
        }

        $result = $resultCaseInfo;
        unset($resultCaseInfo);

        //获取本批次简历数量
        $rRDetailModel = new RRDetailModel();
        $resultCount = $rRDetailModel->getCaseResumeCount($id);

        if ($resultCount) {
            foreach ($resultCount as $key => $item) {
                $caseResumeNum += $item['num'];
                if (isset($result[$item['caseid']])) {
                    $result[$item['caseid']]['num'] = $item['num'] > 999 ? '999+' : $item['num'] . '人';
                }
            }
        }

        $resultAllCount = $rRDetailModel->getCaseAllResumeCount($id, $caseIds["caseid"]);
        if ($resultAllCount) {
            foreach ($resultAllCount as $key => $item) {
                if (isset($result[$item['caseid']])) {
                    $result[$item['caseid']]['status' . $item['status']] = $item['count'] > 999 ? '999+' : $item['count'] . '人';
                }
            }
        }

        return $this->json(
            ['status' => true,
                'message' => 'success',
                'totalCount' => $count,
                'caseResumeNum' => $caseResumeNum > 999 ? '999+' : $caseResumeNum,
                'duetime' => date("Y-m-d", $duetime),
                'resultList' => $result
            ]
        );
    }

    /*
     * 获取已推荐case列表数据
     */
    public function getRecommendCaseListAll(Request $request) {
        //2020-7-6 去除b端职位列表页废弃
        return $this->json(
            ['status' => false,
                'message' => '无此数据',
                'caseinfo' => [],
            ]
        );
        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pageLimit/d', 10);

        $id = $request->getParam('recommendid');
        $subId = $request->getParam('subid');
        $dueTime = $request->getParam('duetime');
        $isTest = $request->getParam('istest', '');

        $caseModel = new CaseModel();
        //获取推荐的case列表数量
        $count = $caseModel->getCaseListCountBySubID($subId);
        $result = [];
        if ($count < 1) {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'totalCount' => 0,
                    'caseResumeNum' => 0,
                    'duetime' => date("Y-m-d", $dueTime),
                    'caselist' => [],
                ]
            );
        }

        list($pageNum, $rowFrom, $rowTo) = $caseModel->paginationParameters($pageNum, $pageLimit);
        $field = '';
        $result = $caseModel->getCaseListBySubID($subId, $field, $rowFrom, $rowTo);
        if (!$result) {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'totalCount' => 0,
                    'caseResumeNum' => 0,
                    'duetime' => date("Y-m-d", $dueTime),
                    'caseinfo' => [],
                ]
            );
        }

        $attributes = [
            'area' => [DictConstant::DD_CITY_COUNTY, '']
        ];

        //翻译城市
        $dict = new Dict();
        $dict->rowTrans($result, $attributes, true, ',', ' ');

        $resultCaseInfo = [];
        $caseIds = '';
        foreach ($result as $key => $item) {
            $item['num'] = 0;
            $item['status01'] = $item['status02'] = $item['status04'] = $item['status05'] = $item['status06'] = '0人';
            $resultCaseInfo[$item['caseid']] = $item;
            $caseIds .= $item['caseid'] . ',';
        }
        $caseIds = trim($caseIds, ',');

        if (empty($isTest)) {
            $recommendModel = new RecommendModel();
            $recommendModel->setRecommendReadTime($id);
        }

        //获取简历数量
        $rRDetailModel = new RRDetailModel();
        //$caseResumeNum = 0;
        $resultAllCount = $rRDetailModel->getCaseAllResumeCountBySubID($subId, $caseIds);
        if ($resultAllCount) {
            foreach ($resultAllCount as $key => $item) {
                //$caseResumeNum += $item['count'];
                if (isset($resultCaseInfo[$item['caseid']])) {
                    $resultCaseInfo[$item['caseid']]['num'] += $item['count'];
                    $resultCaseInfo[$item['caseid']]['status' . $item['status']] = $item['count'] > 999 ? '999+' : $item['count'] . '人';
                }
            }
        }

        //获取本批次简历数量
        $resultCount = $rRDetailModel->getCaseResumeCount($id);

        $caseResumeNum = 0;
        if ($resultCount) {
            foreach ($resultCount as $key => $item) {
                $caseResumeNum += $item['num'];
            }
        }

        $result = [];
        foreach ($resultCaseInfo as $key => $item) {
            $item['num'] = $item['num'] > 999 ? '999+' : $item['num'] . '人';
            $result[] = $item;
        }
        unset($resultCaseInfo);

        return $this->json(
            ['status' => true,
                'message' => 'success',
                'totalCount' => $count,
                'caseResumeNum' => $caseResumeNum > 999 ? '999+' : $caseResumeNum,
                'duetime' => date("Y-m-d", $dueTime),
                'resultList' => $result
            ]
        );
    }

    /*
     * 获取case详情信息
     */
    public function getCaseInfo(Request $request) {
        //2020-7-6 去除b端职位列表页废弃
        return $this->json(
            ['status' => false,
                'message' => '无此数据',
                'caseinfo' => [],
            ]
        );
        $caseid = $request->getParam('caseid');
        if (empty($caseid)) {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'caselist' => [],
                ]
            );
        }
        $caseModel = new CaseModel();
        $field = '';
        $result = $caseModel->getCaseList($field, $caseid);
        if (!$result) {
            return $this->json(
                ['status' => false,
                    'message' => '无此数据',
                    'caseinfo' => [],
                ]
            );
        }

        $attributes = [
            'area' => [DictConstant::DD_CITY_COUNTY, '']
        ];

        $dict = new Dict();
        $dict->rowTrans($result, $attributes, true, ',', ' ');

        foreach ($result as $key => &$item) {
            $item['createdate'] = date("Y-m-d", strtotime($item['createdate']));
            $item['updatedate'] = date("Y-m-d", strtotime($item['updatedate']));
            $item['duedate'] = date("Y-m-d", strtotime($item['duedate']));
        }

        return $this->json(
            ['status' => true,
                'message' => 'success',
                'caselist' => $result
            ]
        );
    }
}